package com.gxa.eshops.admin.controller;


import com.gxa.eshops.admin.entity.domain.Goods;
import com.gxa.eshops.admin.service.impl.GoodsServiceImpl;
import com.gxa.eshps.cores.common.JsonResult;
import com.gxa.eshps.cores.exception.BusinessException;
import com.gxa.eshps.cores.exception.ExceptionCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.Map;
/**
 * <p>
 * 商品表 前端控制器
 * </p>
 * @author wangpeng
 * @since 2022-07-11
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品操作接口")
public class GoodsController {

    @Autowired
    GoodsServiceImpl goodsService;

    @PostMapping("/updateAndDeleteCache")
    @ApiOperation("更新商品信息删除缓存")
    public Map<String ,Object> updateAndDeleteCache(Goods goods){
        //设置商品更新时间为系统当前时间
        goods.setGoodsUpdated(LocalDateTime.now());
        JsonResult jsonResult = new JsonResult();
        Boolean aBoolean = goodsService.updateAndDeleteCache(goods);
        if (aBoolean) {
        jsonResult.setCode("1");
        jsonResult.setMsg("操作成功");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("操作失败");
        }
        return jsonResult.getMap();
    }
    @PostMapping("/selectById")
    @ApiOperation("商品查询byId")
    public Map<String ,Object> selectById(Long id) throws BusinessException {
        Goods byId = goodsService.getById(id);
        JsonResult jsonResult = new JsonResult();
        if (byId != null) {
            jsonResult.setCode("1");
            jsonResult.setData(byId);
        } else {
            throw new BusinessException(ExceptionCode.GOODS_NULL);
        }
        return jsonResult.getMap();
    }
    @PostMapping("/addHotGoodsCache")
    @ApiOperation("添加热点商品缓存")
    public Map<String ,Object> addHotGoodsCache(Long...ids) throws Exception {
        return goodsService.addHotGoodsCache(ids);
    }

}

